package jp.ac.u_tokyo.dch.dao.xmloutput;

import java.util.ArrayList;
import java.util.List;

import jp.ac.u_tokyo.dch.core.db.CustomPreparedStatement;
import jp.ac.u_tokyo.dch.core.db.CustomResultSet;
import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.model.output.Attribute;
import jp.ac.u_tokyo.dch.model.output.XmlData;
import jp.ac.u_tokyo.dch.util.common.CommonDBConnection;

public class OutputTagDao {

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbconn = null;

	/**
	 * 生成者
	 *
	 * @param DBコネクションオブジェクト
	 */
	public OutputTagDao(DBConnection dbconn) {
		this.dbconn = dbconn;
	}


	/**
	 * データを挿入する
	 * @param order 順番
	 * @param schemaid スキーマID
	 * @param xmlData XMLデータ
	 * @return 挿入件数
	 * @throws DBException 例外
	 */
	public int insert(int order, String schemaid, XmlData xmlData) throws DBException {

		CustomPreparedStatement ps = null;
		int result = 0;
		try{
			String query = "INSERT INTO output_tag (schemaid,tagid,tagname,level,oyatagid,text,tagorder,type)" +
					" VALUES (?,?,?,?,?,?,?,?)";

			ps = dbconn.prepareStatement(query);

			int index = 1;
			ps.setInt(index++, Integer.valueOf(schemaid));
			ps.setInt(index++, Integer.valueOf(xmlData.getTagId()));
			ps.setString(index++, xmlData.getTagName());
			ps.setInt(index++, Integer.valueOf(xmlData.getLevel()));
			ps.setInt(index++, Integer.valueOf(xmlData.getOyaTagId()));
			ps.setString(index++, xmlData.getText());
			ps.setInt(index++, order);
			ps.setString(index++, xmlData.getType());

			result = ps.executeUpdate();


		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps);
		}
		return result;
	}

	public int delete(String schemaid) throws DBException {

		CustomPreparedStatement ps = null;
		int result = 0;
		try{
			String query = "DELETE FROM output_tag WHERE schemaid = ?";

			ps = dbconn.prepareStatement(query);

			int index = 1;
			ps.setInt(index++, Integer.valueOf(schemaid));

			result = ps.executeUpdate();

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps);
		}
		return result;
	}

	/**
	 * データを取得する
	 * @param schemaid スキーマID
	 * @return XMLデータ
	 * @throws DBException 例外
	 */
	public List<XmlData> selectXmlData(String schemaid) throws DBException {

		List<XmlData> list = new ArrayList<XmlData>();
		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			String query = "SELECT * FROM output_tag where schemaid = ? order by tagorder";

			ps = dbconn.prepareStatement(query);
			ps.setInt(1, Integer.valueOf(schemaid));
			rs = ps.executeQuery();
			while (rs.next()){

				XmlData xmlData = new XmlData();

				String tagId = rs.getString("tagid");

				xmlData.setAttrList(selectAttrData(schemaid, tagId));

				xmlData.setTagId(tagId);
				xmlData.setTagName(rs.getString("tagname"));
				xmlData.setLevel(rs.getString("level"));
				xmlData.setOyaTagId(rs.getString("oyatagid"));
				xmlData.setText(rs.getString("text"));
				xmlData.setType(rs.getString("type"));

				list.add(xmlData);
			}
			//資料テーブルに現物資料データとのトランザクション処理のためDBコネクションクローズはしない。
		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}

		return list;
	}

	/**
	 * 属性リストを取得する
	 * @param schemaid スキーマID
	 * @param tagId タグID
	 * @return 属性リスト
	 * @throws DBException 例外
	 */
	private List<Attribute> selectAttrData(String schemaid, String tagId) throws DBException {


		List<Attribute> list = new ArrayList<Attribute>();
		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			String query = "SELECT * FROM output_attr where schemaid = ? and tagid = ? order by outputorder";

			ps = dbconn.prepareStatement(query);
			ps.setInt(1, Integer.valueOf(schemaid));
			ps.setInt(2, Integer.valueOf(tagId));
			rs = ps.executeQuery();
			while (rs.next()){

				Attribute attribute = new Attribute();

				attribute.setName(rs.getString("name"));
				attribute.setValue(rs.getString("value"));

				list.add(attribute);
			}
			//資料テーブルに現物資料データとのトランザクション処理のためDBコネクションクローズはしない。
		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}

		return list;
	}

}
